﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Spider.DB;
using Spider.Rules;
using Spider.Analyse;
using Spider.Get;
using Spider.Rules.英语学习资料;
using Spider.HTMLAnalyse.新概念英语HTML解析;
using Spider.Entitys;
using Spider.HTMLAnalyse.英语学习资料HTML解析;

namespace Spider.Init.英语学习资料
{
    public class Init新概念英语第一套 : Iinit
    {
        #region 属性
        /// <summary>
        /// HTML页面解析XPath
        /// </summary>
        IRule rule { get; set; }

        /// <summary>
        /// 页面解析规则
        /// </summary>
        IAnalyse analyse { get; set; }

        /// <summary>
        /// 数据库持久化方式
        /// </summary>
        IDBs db { get; set; }
        #endregion

        #region 设置数据库持久化方式
        /// <summary>
        /// 设置数据库持久化方式
        /// </summary>
        /// <param name="db"></param>
        public void SetDB(IDBs db)
        {
            this.db = db;
        }
        #endregion

        public void Start()
        {
            rule = new 新概念英语分页路径解析();
            analyse = new 新概念英语页面解析();
            List<string> urls = new List<string>()
            {
                "http://www.tingclass.net/list-5043-1.html",
                "http://www.tingclass.net/list-5043-2.html",
                "http://www.tingclass.net/list-5043-3.html",
                "http://www.tingclass.net/list-5043-4.html",
                "http://www.tingclass.net/list-5043-5.html",
                "http://www.tingclass.net/list-5043-6.html",
                "http://www.tingclass.net/list-5043-7.html",
                "http://www.tingclass.net/list-5043-8.html",
            };
            string html;
            List<URLListDTO> list = new List<URLListDTO>();
            //穷举出所有的页面
            foreach (var url in urls)
            {
                html = GetHTML.Get(url);
                list.AddRange(analyse.AnalyseStart<URLListDTO>(html, rule));
            }

            Dictionary<string, string> dic = new Dictionary<string, string>();
            //遍历所有的页面，获取到所有的下载连接地址
            foreach (var item in list)
            {
                item.URL = item.URL.Replace("show", "down");
            }

            foreach (var item in list)
            {
                html = GetHTML.Get(item.URL);
                rule = new tingclassDownLoadXPath();
                analyse = new tingclassDownLoad页面解析();
                analyse.AnalyseStart<URLListDTO>(html, rule, item.Title);
            }



        }


    }
}
